package com.example.javaserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javaserve.dto.FileResponseInfo;
import com.example.javaserve.entity.MyFileInfo;
import com.example.javaserve.entity.UserBaseInfo;
import com.example.javaserve.mapper.MyFileMapper;
import com.example.javaserve.result.Result;
import com.example.javaserve.result.impl.SuccessResult;
import com.example.javaserve.service.FilesService;
import com.example.javaserve.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @projectName: my_project
 * @package: com.example.javaserve.service.impl
 * @className: FilesServiceImpl
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2024/1/5 15:36
 * @version: 1.0
 **/
@Service
public class FilesServiceImpl extends ServiceImpl<MyFileMapper, MyFileInfo> implements FilesService {
    @Autowired
    private MyFileMapper myFileMapper;
    @Autowired
    private FileUtil fileUtil;

    @Override
    public Result<MyFileInfo > getFiles(int current, int size) {
        QueryWrapper<MyFileInfo> o = new QueryWrapper<>();
        o.select("file_name","upload_time","file_size","file_type");
        IPage<MyFileInfo> page = new Page<>(current, size);
        IPage<MyFileInfo> myFileInfoIPage = myFileMapper.selectPage(page, o);
        long total = myFileInfoIPage.getTotal();
        List<MyFileInfo> records = myFileInfoIPage.getRecords();
        for (MyFileInfo record:
             records) {
            String fileName = record.getFileName();
            String originfileName = fileName.substring(fileName.indexOf('-') + 1);
            record.setFileName(originfileName);
        }

        return new SuccessResult<MyFileInfo>(String.valueOf(total),records);
    }
}
